package com.huawei.hianalytics.ab.bc.kl;

import com.huawei.hianalytics.ab.bc.ef.ab;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.zip.Deflater;

public final class de {
    public static String ab(File file) {
        Closeable fileInputStream;
        String str;
        Throwable th;
        try {
            fileInputStream = new FileInputStream(file);
            try {
                ab abVar = new ab(WXMediaMessage.DESCRIPTION_LENGTH_LIMIT);
                byte[] bArr = new byte[WXMediaMessage.DESCRIPTION_LENGTH_LIMIT];
                while (true) {
                    int read = fileInputStream.read(bArr);
                    if (read == -1) {
                        break;
                    }
                    abVar.ab(bArr, read);
                }
                if (abVar.ab() == 0) {
                    str = "";
                    ab(fileInputStream);
                } else {
                    str = new String(abVar.bc(), "UTF-8");
                    ab(fileInputStream);
                }
            } catch (FileNotFoundException e) {
                try {
                    ab.cd("StreamUtil", "getInfoFromFile(): No files need to be read");
                    str = "";
                    ab(fileInputStream);
                    return str;
                } catch (Throwable th2) {
                    th = th2;
                    ab(fileInputStream);
                    throw th;
                }
            } catch (IOException e2) {
                ab.cd("StreamUtil", "getInfoFromFile(): stream.read or new string exception");
                str = "";
                ab(fileInputStream);
                return str;
            }
        } catch (FileNotFoundException e3) {
            fileInputStream = null;
            ab.cd("StreamUtil", "getInfoFromFile(): No files need to be read");
            str = "";
            ab(fileInputStream);
            return str;
        } catch (IOException e4) {
            fileInputStream = null;
            ab.cd("StreamUtil", "getInfoFromFile(): stream.read or new string exception");
            str = "";
            ab(fileInputStream);
            return str;
        } catch (Throwable th3) {
            Throwable th4 = th3;
            fileInputStream = null;
            th = th4;
            ab(fileInputStream);
            throw th;
        }
        return str;
    }

    public static String ab(InputStream inputStream) {
        Closeable byteArrayOutputStream = new ByteArrayOutputStream();
        String str = "";
        try {
            byte[] bArr = new byte[WXMediaMessage.DESCRIPTION_LENGTH_LIMIT];
            while (true) {
                int read = inputStream.read(bArr);
                if (read == -1) {
                    break;
                }
                byteArrayOutputStream.write(bArr, 0, read);
            }
            str = byteArrayOutputStream.toString("UTF-8");
            return str;
        } finally {
            ab(byteArrayOutputStream);
        }
    }

    public static void ab(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                ab.cd("StreamUtil", "closeQuietly(): Exception when closing the closeable!");
            }
        }
    }

    public static void ab(File file, String str) {
        Closeable fileOutputStream;
        Throwable th;
        Closeable closeable = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            try {
                fileOutputStream.write(str.getBytes("UTF-8"));
                fileOutputStream.flush();
                ab(fileOutputStream);
            } catch (FileNotFoundException e) {
                try {
                    ab.cd("StreamUtil", "saveInfoToFile(): No files need to be read");
                    ab(fileOutputStream);
                } catch (Throwable th2) {
                    Throwable th3 = th2;
                    closeable = fileOutputStream;
                    th = th3;
                    ab(closeable);
                    throw th;
                }
            } catch (IOException e2) {
                ab.cd("StreamUtil", "saveInfoToFile(): io exc from write info to file!");
                ab(fileOutputStream);
            }
        } catch (FileNotFoundException e3) {
            fileOutputStream = null;
            ab.cd("StreamUtil", "saveInfoToFile(): No files need to be read");
            ab(fileOutputStream);
        } catch (IOException e4) {
            fileOutputStream = null;
            ab.cd("StreamUtil", "saveInfoToFile(): io exc from write info to file!");
            ab(fileOutputStream);
        } catch (Throwable th4) {
            th = th4;
            ab(closeable);
            throw th;
        }
    }

    private static void ab(OutputStream outputStream) {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                ab.cd("StreamUtil", "closeStream(): Exception: close OutputStream error!");
            }
        }
    }

    public static void ab(HttpURLConnection httpURLConnection) {
        try {
            httpURLConnection.getInputStream().close();
        } catch (Exception e) {
            ab.cd("StreamUtil", "closeQuietly(): Exception when connHttp.getInputStream()!,There may be no network, or no INTERNET permission");
        }
        httpURLConnection.disconnect();
        ab.ab("StreamUtil", " connHttp disconnect");
    }

    public static byte[] ab(byte[] bArr) {
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Deflater deflater = new Deflater();
        deflater.setInput(bArr);
        deflater.finish();
        byte[] bArr2 = new byte[WXMediaMessage.DESCRIPTION_LENGTH_LIMIT];
        while (!deflater.finished()) {
            byteArrayOutputStream.write(bArr2, 0, deflater.deflate(bArr2));
        }
        bArr2 = byteArrayOutputStream.toByteArray();
        deflater.end();
        ab(byteArrayOutputStream);
        return bArr2;
    }
}
